<template>
  <div class="enhanced-theme-toggle">
    <el-tooltip 
      :content="isDark ? '切换到浅色主题' : '切换到深色主题'" 
      placement="bottom"
      :show-after="500"
    >
      <button 
        class="theme-btn"
        :class="{ 'is-dark': isDark, 'is-animating': isAnimating }"
        @click="handleToggle"
        :disabled="isAnimating"
      >
        <!-- 背景圆圈 -->
        <div class="btn-bg"></div>
        
        <!-- 太阳图标 -->
        <div class="sun-container" :class="{ 'hidden': isDark }">
          <div class="sun-center">
            <div class="sun-face">
              <div class="sun-eye left"></div>
              <div class="sun-eye right"></div>
              <div class="sun-mouth"></div>
            </div>
          </div>
          <div class="sun-rays">
            <div class="ray" v-for="i in 8" :key="i" :style="rayStyle(i)"></div>
          </div>
        </div>
        
        <!-- 月亮图标 -->
        <div class="moon-container" :class="{ 'visible': isDark }">
          <div class="moon-body">
            <div class="moon-face">
              <div class="moon-eye left"></div>
              <div class="moon-eye right"></div>
              <div class="moon-mouth"></div>
            </div>
            <div class="crater crater-1"></div>
            <div class="crater crater-2"></div>
            <div class="crater crater-3"></div>
          </div>
        </div>
        
        <!-- 云朵装饰 -->
        <div class="clouds" :class="{ 'show': !isDark }">
          <div class="cloud cloud-1"></div>
          <div class="cloud cloud-2"></div>
        </div>
        
        <!-- 星星装饰 -->
        <div class="stars-decoration" :class="{ 'show': isDark }">
          <div class="star-item" v-for="i in 6" :key="i" :style="starStyle(i)">
            {{ i % 2 === 0 ? '✨' : '⭐' }}
          </div>
        </div>
        
        <!-- 点击波纹效果 -->
        <div class="ripple-effect" :class="{ 'active': isAnimating }"></div>
      </button>
    </el-tooltip>
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
import { useThemeStore } from '@/stores/theme'

const themeStore = useThemeStore()
const isAnimating = ref(false)

const isDark = computed(() => themeStore.isDark)

// 太阳光线样式
const rayStyle = (index) => {
  const angle = (index - 1) * 45
  return {
    transform: `rotate(${angle}deg)`,
    animationDelay: `${index * 0.1}s`
  }
}

// 星星位置样式
const starStyle = (index) => {
  const positions = [
    { top: '15%', left: '20%' },
    { top: '25%', right: '15%' },
    { top: '60%', left: '10%' },
    { top: '70%', right: '20%' },
    { top: '40%', left: '80%' },
    { top: '80%', left: '70%' }
  ]
  
  return {
    ...positions[index - 1],
    animationDelay: `${index * 0.2}s`
  }
}

const handleToggle = async () => {
  if (isAnimating.value) return
  
  isAnimating.value = true
  
  // 添加页面过渡效果
  document.body.style.overflow = 'hidden'
  
  // 切换主题
  themeStore.toggleTheme()
  
  // 动画结束后重置状态
  setTimeout(() => {
    isAnimating.value = false
    document.body.style.overflow = ''
  }, 800)
}
</script>

<style scoped>
.enhanced-theme-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
}

.theme-btn {
  position: relative;
  width: 48px;
  height: 48px;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  overflow: hidden;
  background: transparent;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  outline: none;
  padding: 0;
}

.theme-btn:disabled {
  cursor: not-allowed;
}

.theme-btn:focus {
  outline: 2px solid var(--el-color-primary);
  outline-offset: 2px;
}

/* 背景 */
.btn-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: linear-gradient(135deg, #fbbf24 0%, #f59e0b 100%);
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 4px 12px rgba(251, 191, 36, 0.3);
}

.theme-btn.is-dark .btn-bg {
  background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
}

.theme-btn:hover .btn-bg {
  transform: scale(1.05);
  box-shadow: 0 6px 16px rgba(251, 191, 36, 0.4);
}

.theme-btn.is-dark:hover .btn-bg {
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.5);
}

/* 太阳容器 */
.sun-container {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  transition: all 0.5s cubic-bezier(0.4, 0, 0.2, 1);
  opacity: 1;
  visibility: visible;
}

.sun-container.hidden {
  transform: translate(-50%, -50%) rotate(180deg) scale(0);
  opacity: 0;
  visibility: hidden;
}

.sun-center {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #ffffff;
  position: relative;
  box-shadow: 0 0 12px rgba(255, 255, 255, 0.8);
  animation: sunPulse 3s ease-in-out infinite;
}

.sun-face {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.sun-eye {
  width: 2px;
  height: 2px;
  background: #f59e0b;
  border-radius: 50%;
  position: absolute;
}

.sun-eye.left {
  top: -2px;
  left: -3px;
}

.sun-eye.right {
  top: -2px;
  right: -3px;
}

.sun-mouth {
  width: 4px;
  height: 2px;
  border: 1px solid #f59e0b;
  border-top: none;
  border-radius: 0 0 4px 4px;
  position: absolute;
  top: 1px;
  left: 50%;
  transform: translateX(-50%);
}

.sun-rays {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.ray {
  position: absolute;
  width: 2px;
  height: 8px;
  background: #ffffff;
  border-radius: 1px;
  transform-origin: 50% 18px;
  opacity: 0.9;
  animation: rayRotate 4s linear infinite;
}

/* 月亮容器 */
.moon-container {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0) rotate(-180deg);
  transition: all 0.5s cubic-bezier(0.4, 0, 0.2, 1);
  opacity: 0;
  visibility: hidden;
}

.moon-container.visible {
  transform: translate(-50%, -50%) scale(1) rotate(0deg);
  opacity: 1;
  visibility: visible;
}

.moon-body {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #e2e8f0;
  position: relative;
  box-shadow: 0 0 12px rgba(226, 232, 240, 0.6);
  animation: moonGlow 4s ease-in-out infinite;
}

.moon-face {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.moon-eye {
  width: 2px;
  height: 2px;
  background: #64748b;
  border-radius: 50%;
  position: absolute;
}

.moon-eye.left {
  top: -2px;
  left: -3px;
}

.moon-eye.right {
  top: -2px;
  right: -3px;
}

.moon-mouth {
  width: 3px;
  height: 3px;
  border: 1px solid #64748b;
  border-radius: 50%;
  position: absolute;
  top: 1px;
  left: 50%;
  transform: translateX(-50%);
}

.crater {
  position: absolute;
  border-radius: 50%;
  background: #cbd5e1;
}

.crater-1 {
  width: 3px;
  height: 3px;
  top: 5px;
  left: 7px;
}

.crater-2 {
  width: 2px;
  height: 2px;
  top: 10px;
  left: 12px;
}

.crater-3 {
  width: 2px;
  height: 2px;
  top: 12px;
  left: 6px;
}

/* 云朵装饰 */
.clouds {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  transition: all 0.6s ease;
  pointer-events: none;
}

.clouds.show {
  opacity: 1;
}

.cloud {
  position: absolute;
  background: rgba(255, 255, 255, 0.6);
  border-radius: 10px;
  animation: cloudFloat 6s ease-in-out infinite;
}

.cloud::before,
.cloud::after {
  content: '';
  position: absolute;
  background: rgba(255, 255, 255, 0.6);
  border-radius: 50%;
}

.cloud-1 {
  width: 8px;
  height: 4px;
  top: 8px;
  right: 6px;
  animation-delay: 0s;
}

.cloud-1::before {
  width: 4px;
  height: 4px;
  top: -2px;
  left: 1px;
}

.cloud-1::after {
  width: 3px;
  height: 3px;
  top: -1px;
  right: 1px;
}

.cloud-2 {
  width: 6px;
  height: 3px;
  bottom: 10px;
  left: 8px;
  animation-delay: 3s;
}

.cloud-2::before {
  width: 3px;
  height: 3px;
  top: -1px;
  left: 1px;
}

.cloud-2::after {
  width: 2px;
  height: 2px;
  top: -1px;
  right: 1px;
}

/* 星星装饰 */
.stars-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  transition: all 0.8s ease;
  pointer-events: none;
}

.stars-decoration.show {
  opacity: 1;
}

.star-item {
  position: absolute;
  font-size: 6px;
  animation: starTwinkle 2s ease-in-out infinite alternate;
}

/* 波纹效果 */
.ripple-effect {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 0;
  height: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.3);
  transform: translate(-50%, -50%);
  transition: all 0.4s ease;
}

.ripple-effect.active {
  width: 60px;
  height: 60px;
  opacity: 0;
}

/* 动画 */
@keyframes sunPulse {
  0%, 100% {
    box-shadow: 0 0 12px rgba(255, 255, 255, 0.8);
  }
  50% {
    box-shadow: 0 0 20px rgba(255, 255, 255, 1);
  }
}

@keyframes moonGlow {
  0%, 100% {
    box-shadow: 0 0 12px rgba(226, 232, 240, 0.6);
  }
  50% {
    box-shadow: 0 0 20px rgba(226, 232, 240, 0.8);
  }
}

@keyframes rayRotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

@keyframes cloudFloat {
  0%, 100% {
    transform: translateX(0);
  }
  50% {
    transform: translateX(2px);
  }
}

@keyframes starTwinkle {
  0% {
    opacity: 0.3;
    transform: scale(0.8);
  }
  100% {
    opacity: 1;
    transform: scale(1.2);
  }
}

/* 按钮状态 */
.theme-btn.is-animating {
  pointer-events: none;
}

.theme-btn.is-animating .btn-bg {
  animation: buttonPulse 0.8s ease;
}

@keyframes buttonPulse {
  0%, 100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.1);
  }
}

/* 响应式 */
@media (max-width: 768px) {
  .theme-btn {
    width: 44px;
    height: 44px;
  }
  
  .sun-center {
    width: 18px;
    height: 18px;
  }
  
  .moon-body {
    width: 20px;
    height: 20px;
  }
  
  .ray {
    height: 6px;
    transform-origin: 50% 15px;
  }
}
</style>
